package tt.dz.egobus.servicemanager.core.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;
import tt.dz.egobus.api.commoncenter.emuns.ConfigType;

/**
 * @author Anson
 * @date 17-8-8
 * @name
 * @since 1.0.6
 */
public abstract class AbstractConfigAccess implements ConfigAccess {

    public static final String MODULE_NAME = ConfigType.SERVER_MANAGER.getMsg();

    public static final String KEY_PATH_SPLIT = "/";

    @Override
    public Converter<String, String> keyConvert(final String module, final ConfigAccessType type) {
        return new Converter<String, String>() {
            @Override
            public String convert(String source) {
                if (StringUtils.isBlank(source)) {
                    return source;
                }
                ConfigAccessType t = null;
                t = type == null ? ConfigAccessType.SYSTEM : type;
                return t.getKey() + KEY_PATH_SPLIT + module + KEY_PATH_SPLIT + source;
            }
        };
    }

    @Override
    public <T> T value(String key, Class<T> clazz) {
        return this.value(key, (T) null);
    }


    /**
     * @param e
     * @return
     */
    protected String keyRuntimeConfigKeyEmunConvert(RuntimeConfigKeyEnum e) {
        if (e == null) {
            return "";
        }
        return this.keyConvert(MODULE_NAME, e.getType()).convert(e.getKey());
    }

}
